package cn.tedu.property.management.service.impl;

import cn.tedu.property.management.ex.ServiceException;
import cn.tedu.property.management.mapper.HouseMapper;
import cn.tedu.property.management.pojo.dto.HouseAddNewDTO;
import cn.tedu.property.management.pojo.dto.HouseUpdateDTO;
import cn.tedu.property.management.pojo.entity.Community;
import cn.tedu.property.management.pojo.entity.House;
import cn.tedu.property.management.pojo.vo.CommunityStandardVO;
import cn.tedu.property.management.pojo.vo.HouseListVO;
import cn.tedu.property.management.pojo.vo.HouseStandardVO;
import cn.tedu.property.management.service.IHouseService;
import cn.tedu.property.management.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
@Service
@Slf4j
public class HouseServiceImpl implements IHouseService {

    @Autowired
    private HouseMapper houseMapper;
    @Value("${house.expense.unit-price}")
    private BigDecimal price;

    @Override
    public void addNew(HouseAddNewDTO houseAddNewDTO) {
        log.info("开始处理【添加房屋】请求，参数：{}",houseAddNewDTO);
        //判断是否已经添加过
        String unit = houseAddNewDTO.getUnit();
        String building = houseAddNewDTO.getBuilding();
        String floor = houseAddNewDTO.getFloor();
        String number = houseAddNewDTO.getNumber();
        int row = houseMapper.countByNumber(unit, building, floor, number);
        if(row > 0){
            String message = "该房屋已存在！";
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,message);
        }
        houseAddNewDTO.setExpense(houseAddNewDTO.getExpense().multiply(price));
        int i = houseMapper.addNew(houseAddNewDTO);
        if(i==0){
            log.info("房屋添加失败！");
            return;
        }
        log.info("房屋添加成功！");
    }

    @Override
    public void deleteByNumber(String unit, String building, String floor, String number) {
        log.info("开始处理【删除房屋】的请求");
        //判断房屋是否存在
        int row = houseMapper.countByNumber(unit, building, floor, number);
        if(row==0){
            String message = "该房屋不存在！";
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,message);
        }
        int i = houseMapper.deleteByNumber(unit, building, floor, number);
        if(i==0){
            log.info("房屋删除失败！");
            return;
        }
        log.info("房屋删除成功！");
    }

    @Override
    public void update(Long id,HouseUpdateDTO houseUpdateDTO) {
        int row = houseMapper.countById(id);
        if(row==0){
            String message = "更新失败，尝试更新的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        House house = new House();
        BeanUtils.copyProperties(houseUpdateDTO,house);
        house.setId(id);
        int rows = houseMapper.update(house);
        if(rows!=1){
            String message = "更新失败，服务器繁忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public Integer countByState(Integer state) {
        String[] states = new String[3];
        states[0] = "未售";
        states[1] = "未入住";
        states[2] = "已入住";
        int count = houseMapper.countByState(state);
        log.info("房屋状态为{}的有{}个",states[state],count);
        return count;
    }

    @Override
    public HouseStandardVO getStandardByNumber(String unit, String building, String floor, String number) {
        HouseStandardVO houseStandardVO = houseMapper.getStandardByNumber(unit, building, floor, number);
        if(houseStandardVO==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"您要查询的房屋不存在！");
        }
        return houseStandardVO;
    }

    @Override
    public List<HouseListVO> getListByState(Integer state) {
        return houseMapper.getListByState(state);
    }

    @Override
    public List<HouseListVO> getListByOwnerId(Long ownerId) {
        return houseMapper.getListByOwnerId(ownerId);
    }
}
